package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OtherFeeBiz;
import com.egao.orderB.entity.OtherFee;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-11-17 15:26:59
 * @version ：1.0
 */
@Repository
public class OtherFeeBizImpl extends BaseDao implements OtherFeeBiz {

	@Override
	public JsonResult list(PageResult2<T> page, OtherFee otherFee, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OtherFee> rm = new BeanPropertyRowMapper<OtherFee>(OtherFee.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.table_name");
    sql.append(",o.table_id,o.type,o.fee_name,o.price");
		sql.append(",o.standard,o.fee_state,o.fee_person,o.fee_time");
		sql.append(",o.fobject,o.fee_modules,o.invoiceid,o.invoicename");
		sql.append(",o.driver_name,o.invoicenum,o.invoicestate,o.currency");
		sql.append(",o.exchange_rate,o.settlement,o.settlement_price,o.remark");
		sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_other_fee o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 and o.table_name=:table_name and o.table_id=:table_id ");
		map.put("table_name", otherFee.getTableName());
		map.put("table_id", otherFee.getTableId());
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(otherFee)){
//			if(Utils.isNotEmpty(otherFee.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", otherFee.getName());
//			}
      if(Utils.isNotEmpty(otherFee.getSort())&&Utils.isNotEmpty(otherFee.getOrder())){
        sql.append(" order by o."+otherFee.getSort()+" "+otherFee.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OtherFee> list(OtherFee otherFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OtherFee> rm = new BeanPropertyRowMapper<OtherFee>(OtherFee.class);
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.table_name");
    sql.append(",o.table_id,o.type,o.fee_name,o.price");
		sql.append(",o.standard,o.fee_state,o.fee_person,o.fee_time");
		sql.append(",o.fobject,o.fee_modules,o.invoiceid,o.invoicename");
		sql.append(",o.driver_name,o.invoicenum,o.invoicestate,o.currency");
		sql.append(",o.exchange_rate,o.settlement,o.settlement_price,o.remark");
		sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_other_fee o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
		sql.append(" where o.deleted=0 and o.table_name=:table_name and o.table_id=:table_id ");
		map.put("table_name", otherFee.getTableName());
		map.put("table_id", otherFee.getTableId());
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(otherFee)){
      if(Utils.isNotEmpty(otherFee.getSort())&&Utils.isNotEmpty(otherFee.getOrder())){
        sql.append(" order by o."+otherFee.getSort()+" "+otherFee.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OtherFee getById(OtherFee otherFee) {
		RowMapper<OtherFee> rm = new BeanPropertyRowMapper<OtherFee>(OtherFee.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.table_name");
    sql.append(",o.table_id,o.type,o.fee_name,o.price");
		sql.append(",o.standard,o.fee_state,o.fee_person,o.fee_time");
		sql.append(",o.fobject,o.fee_modules,o.invoiceid,o.invoicename");
		sql.append(",o.driver_name,o.invoicenum,o.invoicestate,o.currency");
		sql.append(",o.exchange_rate,o.settlement,o.settlement_price,o.remark");
		sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_other_fee o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 and o.id=:id ");
		map.put("id", otherFee.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(OtherFee otherFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_other_fee (");
    sql.append("organization_id,table_name,table_id,type");
		sql.append(",fee_name,price,standard,fee_state");
		sql.append(",fee_person,fee_time,fobject,fee_modules");
		sql.append(",invoiceid,invoicename,driver_name,invoicenum");
		sql.append(",invoicestate,currency,exchange_rate,settlement");
		sql.append(",settlement_price,remark,create_user,create_time) values ( ");
    sql.append(":organization_id,:table_name,:table_id,:type");
		sql.append(",:fee_name,:price,:standard,:fee_state");
		sql.append(",:fee_person,:fee_time,:fobject,:fee_modules");
		sql.append(",:invoiceid,:invoicename,:driver_name,:invoicenum");
		sql.append(",:invoicestate,:currency,:exchange_rate,:settlement");
		sql.append(",:settlement_price,:remark,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("table_name", otherFee.getTableName());
    map.put("table_id", otherFee.getTableId());
    map.put("type", otherFee.getType());
    map.put("fee_name", otherFee.getFeeName());
    map.put("price", otherFee.getPrice());
    map.put("standard", otherFee.getStandard());
    map.put("fee_state", otherFee.getFeeState());
    map.put("fee_person", otherFee.getFeePerson());
    map.put("fee_time", otherFee.getFeeTime());
    map.put("fobject", otherFee.getFobject());
    map.put("fee_modules", otherFee.getFeeModules());
    map.put("invoiceid", otherFee.getInvoiceid());
    map.put("invoicename", otherFee.getInvoicename());
    map.put("driver_name", otherFee.getDriverName());
    map.put("invoicenum", otherFee.getInvoicenum());
    map.put("invoicestate", otherFee.getInvoicestate());
    map.put("currency", otherFee.getCurrency());
    map.put("exchange_rate", otherFee.getExchangeRate());
    map.put("settlement", otherFee.getSettlement()==null?null:otherFee.getSettlement()+"");
    map.put("settlement_price", otherFee.getSettlementPrice());
    map.put("remark", otherFee.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OtherFee otherFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_other_fee set ");
		sql.append("fee_name=:fee_name,price=:price,standard=:standard ");
		sql.append(",fee_state=:fee_state,fee_person=:fee_person,fee_time=:fee_time ");
		sql.append(",fobject=:fobject,fee_modules=:fee_modules,invoiceid=:invoiceid ");
		sql.append(",invoicename=:invoicename,driver_name=:driver_name,invoicenum=:invoicenum ");
		sql.append(",invoicestate=:invoicestate,currency=:currency,exchange_rate=:exchange_rate ");
		sql.append(",settlement=:settlement,settlement_price=:settlement_price,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("fee_name", otherFee.getFeeName());
    map.put("price", otherFee.getPrice());
    map.put("standard", otherFee.getStandard());
    map.put("fee_state", otherFee.getFeeState());
    map.put("fee_person", otherFee.getFeePerson());
    map.put("fee_time", otherFee.getFeeTime());
    map.put("fobject", otherFee.getFobject());
    map.put("fee_modules", otherFee.getFeeModules());
    map.put("invoiceid", otherFee.getInvoiceid());
    map.put("invoicename", otherFee.getInvoicename());
    map.put("driver_name", otherFee.getDriverName());
    map.put("invoicenum", otherFee.getInvoicenum());
    map.put("invoicestate", otherFee.getInvoicestate());
    map.put("currency", otherFee.getCurrency());
    map.put("exchange_rate", otherFee.getExchangeRate());
    map.put("settlement", otherFee.getSettlement()==null?null:otherFee.getSettlement()+"");
    map.put("settlement_price", otherFee.getSettlementPrice());
    map.put("remark", otherFee.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", otherFee.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(OtherFee otherFee, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(otherFee.getId().length()>0){
			for(String ID : otherFee.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_other_fee set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
